package com.schoolvisitors.controller.admin;

import com.alibaba.fastjson2.JSONObject;
import com.schoolvisitors.CheckPermission;
import com.schoolvisitors.common.PageResult;
import com.schoolvisitors.context.AdminContext;
import com.schoolvisitors.context.UserContext;
import com.schoolvisitors.dto.CreateInstrumentDto;
import com.schoolvisitors.entity.*;
import com.schoolvisitors.enums.PermissionEnum;
import com.schoolvisitors.enums.ReservationTypeEnum;
import com.schoolvisitors.response.ResultBody;
import com.schoolvisitors.service.InstrumentService;
import com.schoolvisitors.vo.InstrumentLoanVo;
import com.schoolvisitors.vo.ManageInstrumentVo;
import com.schoolvisitors.vo.ReservationVo;
import jakarta.validation.Valid;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/admin")
public class InstrumentManageController {

    private final InstrumentService instrumentService;

    public InstrumentManageController(InstrumentService instrumentService) {
        this.instrumentService = instrumentService;
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @PostMapping("/createInstrument")
    public ResultBody createInstrument(@RequestBody @Valid CreateInstrumentDto dto) {
        instrumentService.createInstrument(dto);
        return ResultBody.success();
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @PostMapping("/updateInstrument")
    public ResultBody updateInstrument(@RequestBody @Valid CreateInstrumentDto dto) {
        instrumentService.updateInstrument(dto);
        return ResultBody.success();
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @PostMapping("/deleteInstrument")
    public ResultBody deleteInstrument(@RequestBody JSONObject json) {
        Long id = json.getLong("id");
        instrumentService.deleteInstrument(id);
        return ResultBody.success();
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @GetMapping("/getInstruments")
    public ResultBody getInstruments(
            @RequestParam("page") int page,
            @RequestParam("size") int size) {

        Pageable pageable = PageRequest.of(page - 1, size, Sort.by(Sort.Order.desc("id")));
        Page<Instrument> reservations = instrumentService.getInstruments(pageable);
        Page<ManageInstrumentVo> voPages = reservations.map(ManageInstrumentVo::toVo);

        return ResultBody.success(PageResult.bind(voPages));
    }

//    @GetMapping("/getInstruments")
//    public ResultBody getInstruments() {
//        Page<Instrument> instruments = instrumentService.getInstruments();
//        return ResultBody.success(instruments);
//    }





    @GetMapping("/getInstrumentLoans")
    public ResultBody getInstrumentLoans(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        Admin admin = AdminContext.get();
        Pageable pageable = PageRequest.of(page - 1, size, Sort.by(Sort.Order.desc("id")));
        Page<InstrumentLoan> pages = instrumentService.getInstrumentLoansByReviewer(admin.getId(), pageable);
        Page<InstrumentLoanVo> voPages = pages.map(InstrumentLoanVo::toVo);
        return ResultBody.success(PageResult.bind(voPages));
    }

    @GetMapping("/getInstrumentLoansByNotReview")
    public ResultBody getInstrumentLoansByNotReview(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        Admin admin = AdminContext.get();
        Pageable pageable = PageRequest.of(page - 1, size, Sort.by(Sort.Order.desc("id")));
        Page<InstrumentLoan> pages = instrumentService.getInstrumentLoansByNotReview(admin.getId(), pageable);
        Page<InstrumentLoanVo> voPages = pages.map(InstrumentLoanVo::toVo);
        return ResultBody.success(PageResult.bind(voPages));
    }

    @PostMapping("/approveInstrumentLoanByUp")
    public ResultBody approveInstrumentLoanByUp(@RequestBody Map<String, Long> body) {
        Admin admin = AdminContext.get();
        Long id = body.get("id");
        Long points = body.get("points");
        instrumentService.approveInstrumentLoanByUp(admin.getId(), id, points);
        return ResultBody.success();
    }

    @PostMapping("/approveInstrumentLoan")
    public ResultBody approve(@RequestBody Map<String, Long> body) {
        Admin admin = AdminContext.get();
        Long id = body.get("id");
        instrumentService.approveInstrumentLoan(admin.getId(), id);
        return ResultBody.success();
    }

    @PostMapping("/rejectInstrumentLoan")
    public ResultBody reject(@RequestBody Map<String, Long> body) {
        Admin admin = AdminContext.get();
        Long id = body.get("id");
        instrumentService.rejectInstrumentLoan(admin.getId(), id);
        return ResultBody.success();
    }
}
